﻿using JUtil;
using JUtil.Extensions;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace SqlConnectionSettings
{
    public class SqlFileExporter
    {

        public void Add(SqlInfo info)
        {
            infos.Add(info);
        }

        List<SqlInfo> infos = new List<SqlInfo>();

        public void Export(string path)
        {
            JUtil.Path.File.Delete(path);

            StringBuilder sb = new StringBuilder();

            foreach (SqlInfo info in infos)
            {
                /*
                    [GetOrderBy]
                    ORDER BY ACTIVITY_ID DESC
                    --------------------------------------------------------------------------------
                 */
                sb.Append(string.Format("[{0}]", info.section));
                sb.Append("\n");
                if (string.IsNullOrEmpty(info.comment))
                {
                    sb.Append(info.comment);
                    sb.Append("\n");
                }
                sb.Append(info.block);
                sb.Append("\n");
                sb.Append("--------------------------------------------------------------------------------");
                sb.Append("\n");
            }

            sb.ToString().SaveAs(path);
        }


    }

    public class SqlInfo
    {
        private string _section;
        public string section
        {
            get
            {
                return _section.Trim();
            }
            set
            {
                _section = value.Trim();
            }
        }

        private string _block;
        public string block
        {
            get
            {
                return _block.Trim();
            }
            set
            {
                _block = value.Trim();
            }
        }

        private string _comment;
        public string comment
        {
            get
            {
                return _comment.Trim();
            }
            set
            {
                _comment = value.Trim();
            }
        }

    }
}
